//
//  GTNormalTableViewCell.m
//  news
//
//  Copyright © 2022 sinosun. All rights reserved.
//

#import "GTNormalTableViewCell.h"
#import "GTListItem.h"
#import "SDWebImage.h"

@interface GTNormalTableViewCell ()
@property (nonatomic, strong, readwrite) UILabel *titleLabel;
@property (nonatomic, strong, readwrite) UILabel *sourceLabel;
@property (nonatomic, strong, readwrite) UILabel *commentLabel;
@property (nonatomic, strong, readwrite) UILabel *timeLabel;
@property (nonatomic, strong, readwrite) UIImageView *rightImageView;
@property (nonatomic, strong, readwrite) UIButton *rightButton;
@end

@implementation GTNormalTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:({
            self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 250, 50)];
            //self.titleLabel.backgroundColor = [UIColor redColor];
            self.titleLabel.font = [UIFont systemFontOfSize:16];
            self.titleLabel.textColor = [UIColor blackColor];
            self.titleLabel.numberOfLines = 2;
            self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
            self.titleLabel;
        })];
        
        [self.contentView addSubview:({
            self.sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 30, 20)];
            //self.sourceLabel.backgroundColor = [UIColor redColor];
            self.sourceLabel.font = [UIFont systemFontOfSize:12];
            self.sourceLabel.textColor = [UIColor lightGrayColor];
            self.sourceLabel;
        })];
        
        [self.contentView addSubview:({
            self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 80, 30, 20)];
            //self.commentLabel.backgroundColor = [UIColor redColor];
            self.commentLabel.font = [UIFont systemFontOfSize:12];
            self.commentLabel.textColor = [UIColor lightGrayColor];
            self.commentLabel;
        })];
        
        [self.contentView addSubview:({
            self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 80, 30, 20)];
            self.timeLabel.backgroundColor = [UIColor redColor];
            self.timeLabel.font = [UIFont systemFontOfSize:12];
            //self.timeLabel.textColor = [UIColor lightGrayColor];
            self.timeLabel;
        })];
        
        [self.contentView addSubview:({
            self.rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(300, 15, 70, 70)];
            self.rightImageView.backgroundColor = [UIColor lightGrayColor];
            self.rightImageView;
        })];
        
        [self.contentView addSubview:({
            self.rightButton = [[UIButton alloc] initWithFrame:CGRectMake(275, 80, 20, 20)];
            self.rightButton.backgroundColor = [UIColor blueColor];
            [self.rightButton setTitle:@"X" forState:(UIControlStateNormal)];
            [self.rightButton setTitle:@"Y" forState:(UIControlStateHighlighted)];
            
            // 修改 layer
            self.rightButton.layer.cornerRadius = 10;
            self.rightButton.layer.masksToBounds = YES;
            self.rightButton.layer.borderColor = [UIColor blackColor].CGColor;
            self.rightButton.layer.borderWidth = 5;
            
            [self.rightButton addTarget:self action:@selector(rigthButtonClicket) forControlEvents:UIControlEventTouchUpInside];
            self.rightButton;
        })];
    }
    return self;
}

- (void)layoutTableViewCell{
    self.titleLabel.text = @"大标题";
    
    self.sourceLabel.text = @"sourceLabel";
    [self.sourceLabel sizeToFit];
    
    self.commentLabel.text = @"commentLabel";
    [self.commentLabel sizeToFit];
    self.commentLabel.frame = CGRectMake(self.sourceLabel.frame.origin.x + self.sourceLabel.frame.size.width + 15, self.commentLabel.frame.origin.y, self.commentLabel.frame.size.width, self.commentLabel.frame.size.height);
    
    self.timeLabel.text = @"timeLabel";
    [self.timeLabel sizeToFit];
    self.timeLabel.frame = CGRectMake(self.commentLabel.frame.origin.x + self.commentLabel.frame.size.width + 15, self.timeLabel.frame.origin.y, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height);
}



- (void)layoutTableViewCellWithGTListItem:(GTListItem *)listitem
{
    // 如果已读，则修改标题文字颜色
    BOOL hasRead = [[NSUserDefaults standardUserDefaults] boolForKey:listitem.uniquekey];
    if (hasRead) {
        self.titleLabel.textColor = [UIColor lightGrayColor];
    }else{
        self.titleLabel.textColor = [UIColor blackColor];
    }
    
    self.titleLabel.text = listitem.title;
    
    self.sourceLabel.text = listitem.category;
    [self.sourceLabel sizeToFit];
    
    self.commentLabel.text = listitem.authorName;
    [self.commentLabel sizeToFit];
    self.commentLabel.frame = CGRectMake(self.sourceLabel.frame.origin.x + self.sourceLabel.frame.size.width + 15, self.commentLabel.frame.origin.y, self.commentLabel.frame.size.width, self.commentLabel.frame.size.height);
    
    self.timeLabel.text = listitem.date;
    [self.timeLabel sizeToFit];
    self.timeLabel.frame = CGRectMake(self.commentLabel.frame.origin.x + self.commentLabel.frame.size.width + 15, self.timeLabel.frame.origin.y, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height);
    
    // 使用 NSThread 创建一个线程，执行耗时操作
//    NSThread *imageThread = [[NSThread alloc] initWithBlock:^{
//        UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:listitem.pictureUrl]]];
//        self.rightImageView.image = image;
//    }];
//    imageThread.name = @"imageThread";
//    [imageThread start];
    
    
    if (false) {
        // 获取一个默认优先级的全局队列
        dispatch_queue_global_t downloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        // 获取主队列
        dispatch_queue_main_t mainQueue = dispatch_get_main_queue();
        
        dispatch_async(downloadQueue, ^{
            // 在非主线程下载资源
            UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:listitem.pictureUrl]]];
            dispatch_async(mainQueue, ^{
                // 在主线程UI线程渲染图片
                self.rightImageView.image = image;
            });
        });
    }
    
    // SDWebImage 内部会进行 是否已经图片已经加载 ？使用缓存 : 从网络下载。 的操作
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:listitem.pictureUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        NSLog(@"");
        
    }];
    
    
    
}

#pragma mark - 18.2 tagert-action 设计模式
/**
 为 UIbutton 添加一个触发条件，当这个条件触发的时候，通过 开发者传入的 taget, 让 target 调用 action
 */


- (void)rigthButtonClicket{
    NSLog(@"rightButton clicked");
    if (self.deleagte && [self.deleagte respondsToSelector:@selector(tableViewCell:clickDeleteButton:)]) {
        [self.deleagte tableViewCell:self clickDeleteButton:self.rightButton];
    }
}
@end
